<?php

namespace App\Http\Controllers;

use App\Image;
use App\Imgur;
use Illuminate\Http\Request;

use App\Http\Requests;

class ImgurController extends Controller
{
    public function index(){
        $imgurs = Imgur::with('images')->whereNull('deleted_at')->orderBy('created_at','desc')->paginate(60);
        //dd($imgurs[0]->images);
        return view('imgurs.index', compact('imgurs'));
    }

    public function edit(int $imgur){
        $imgur = Imgur::with('images')->find($imgur)->toJson();
       // dd($imgur);
        return view('imgurs.edit', compact('imgur'));
    }

    public function update(Request $request){
        $id = $request->input('id');
        $title_translation = $request->input('title_translation');
        $is_published = $request->input('is_published');
        Imgur::where(compact('id'))->update(compact('title_translation','is_published'));

        if($is_published){
            $imgs = $request->input('images');
            foreach ($imgs as $ig){
                $img_id = $ig['id'];
                Image::where(['id'=>$img_id])->update(['description'=>$ig['description']]);
            }
        }

        //删除

        $deleted_at = $request->input('deleted_at');

        if($deleted_at == 'deleted'){
            $deleted_at = date('Y-m-d H:i:s');
            Imgur::whereId($id)->update(compact('deleted_at'));
            Image::whereImgurId($id)->delete();
        }

        $data =  [];
        return response()->json($data);


    }
}
